import { Modal } from 'ant-design-vue';
let boxCount = null; //定义实例
//定义一个创建实例message 的方法 resetMessage

function createModal(modalType, options){
  if (!options.cancelText) {
    options.cancelText = "取消";
  }
  if (!options.okText) {
    options.okText = "确认";
  }
  if (boxCount) {
    boxCount.destroy();
    boxCount = null;
  }
  return new Promise(async (resolve, reject) => {
    boxCount = Modal[modalType]({
      onCancel() {
        reject();
        boxCount.destroy()
        boxCount = null;
      },
      onOk() {
        resolve(null);
        boxCount.destroy()
        boxCount = null;
      },
      ...options
    })
  })
}

let ModalBox = {
  /**
   * 详情
   * @param content
   * @param title
   * @param options
   */
  info(content, title, options = {}) {
    return createModal("info", {content, title, ...options})
  },
  /**
   * 详情
   * @param content
   * @param title
   * @param options
   */
  success(content, title, options = {}) {
    return createModal("success", {content, title, ...options})
  },
  /**
   * 错误
   * @param content
   * @param title
   * @param options
   */
  error(content, title, options = {}) {
    return createModal("error", {content, title, ...options})
  },
  /**
   * 警告
   * @param content
   * @param title
   * @param options
   */
  warning(content, title, options = {}) {
    return createModal("warning", {content, title, ...options})
  },
  /**
   * 弹窗警告
   * @param content
   * @param title
   * @param options
   */
  confirm(content, title, options = {}) {
    return createModal("confirm", {content, title, ...options})
  },
}

export default ModalBox;